<?php
/**
* @author Tibor Pino (tibor.pino@gmail.com)
* @license released under GNU Public License v. 2
* @copyright Copyright (c) 2007, Tibor Pino
*
*/

class AppController extends Controller
{
    var $helpers = array('AdminMenu','Html','Number','Javascript','Texy','Rating','Date','Form','Pagination','Ajax');
    var $uses = array('Sidebox','Setting','News','Category','Tag','Comment','Download','Theme','Article');
    var $components = array("RequestHandler",'Auth','Cookie');
    var $view = 'Theme';
    var $webroot = "ahoj";

    function beforeFilter()
    {
        $this->setDefaultSettings();
        $this->setAuthComponent();
        $this->setTheme();
        //$this->setAcl();

        if($this->RequestHandler->isAjax()) {
            $this->layout = "ajax";
        } else if(@$this->params[Configure::read('Routing.admin')]) {
            $this->layout = 'admin';
            $this->pageTitle = __('ADMIN_PAGE_TITLE',true);
        } else {
            $this->setSideboxes();
        }
    }

    function setDefaultSettings() {
        $defaultSettings = $this->Setting->findById(1);

        Configure::write('Homepage.news',$defaultSettings['Setting']['homepagenews']);
        Configure::write('Homepage.articles',$defaultSettings['Setting']['homepagearticles']);
        Configure::write('Homepage.downloads',$defaultSettings['Setting']['homepagedownloads']);
        Configure::write('Homepage.comments',$defaultSettings['Setting']['homepagecomments']);
        Configure::write('Date.short',$defaultSettings['Setting']['dateshort']);
        Configure::write('Date.long',$defaultSettings['Setting']['datelong']);
        Configure::write('Page.author',$defaultSettings['Setting']['pageauthor']);
        Configure::write('Page.url',$defaultSettings['Setting']['pageurl']);
        Configure::write('Captcha.question',$defaultSettings['Setting']['captchaquestion']);
        Configure::write('Captcha.answer',$defaultSettings['Setting']['captchaanswer']);
        Configure::write('Statistics.displayed.items',3);
        $this->pageTitle = $defaultSettings['Setting']['pagetitle'];
        $feedburner = $defaultSettings['Setting']['feedburner'];
        $googlecse = $defaultSettings['Setting']['googlecse'];
        $this->set(compact('feedburner','googlecse'));
    }

    function setAuthComponent() {
                $this->Auth->authorize = 'actions';
        if(@$this->params[Configure::read('Routing.admin')]) {
            $this->Auth->loginRedirect = array('controller'=>'statistics','action'=>'index');
        } else {
            $this->Auth->loginRedirect = array('controller' => 'articles', 'action' => 'index');
        }

        $this->Auth->logoutRedirect = '/';
                $this->Auth->allow(array('index','login','logout'));

        $this->Auth->allow(array('display','index','view','get','rss','rating','comment_add','comment_preview'));
        $this->Auth->userScope = array('User.active' => 1);
    }

    function checkAuth() {
        var_dump('here');
    }

    function isAuthorized() {
        if (isset($this->params[Configure::read('Routing.admin')])) {
            if ($this->Auth->user('group_id') != 1) {
                return false;
            }
        }
        $user = array('id'=>$this->Auth->user('id'));
        $isAdmin = true;
        $this->set(compact('isAdmin','user'));
        return true;
    }

    function setSideboxes() {
        $this->set('leftSideboxes',$this->Sidebox->findLeftSideboxes());
        $this->set('middleSideboxes',$this->Sidebox->findMiddleSideboxes());
        $this->set('rightSideboxes',$this->Sidebox->findRightSideboxes());

        //obsah sideboxov
        $this->setNews();
        $this->setCategories();
        $this->setTags();
        $this->setComments();
        $this->setDownloads();
        $this->setBestArticles();
    }

    function setNews() {
        $this->set('sidebox_news',$this->News->findAll(null,null,'News.created desc',Configure::read('Homepage.news')));
    }

    function setCategories() {
        $this->set('sidebox_categories',$this->Category->findAll());
    }

    function setTags() {
        $this->set('sidebox_tags',$this->Tag->findAll());
    }

    function setComments() {
        $this->set('sidebox_comments',$this->Comment->findAll(array('Comment.antispam is null or Comment.antispam=""'),array('Comment.user_name','Comment.id','Article.created','Article.seo_link','Article.title'),'Comment.created desc',Configure::read('Homepage.comments')));
    }

    function setDownloads() {
        $this->set('sidebox_downloads',$this->Download->findAll(null,array('Download.summary','Download.file','Download.size'),'Download.id desc',Configure::read('Homepage.downloads')));
    }

    function setTheme() {
        $theme = $this->Theme->find('Theme.active=1','folder');
        if(empty($theme['Theme']['folder'])) {
            $this->theme = 'default';
        } else {
            $this->theme = $theme['Theme']['folder'];
        }
    }

    function setBestArticles() {
        $this->set('sidebox_articles',$this->Article->findAll(array('Article.visible=1','Article.publish_up<=now()'),array('seo_link','title','created'),'Article.visited desc',5,1,0));
    }

    function redirect_to($message=null,&$object,$action='index') {
        $object->Session->setFlash($message);
        $object->redirect(array('action'=>$action),null,true);
    }

    function setAcl() {
        //$this->setAros();
        //$this->setAcos();
//        $this->setPermissions();
//var_dump($this->Acl->check('admin/Administrator','articles','create'));

    }

    function setAros() {
        $aro = new Aro();
        $groups = array(
            0 => array('alias' => 'admin'),
            1 => array('alias' => 'writer'),
            2 => array('alias' => 'user'),
            3 => array('alias' => 'visitor'),
            4 => array(
                'alias' => 'Administrator',
                'parent_id' => 1,
                'model' => 'User',
                'foreign_key' => 1
            )
        );

        foreach($groups as $group) {
            $aro->create();
            $aro->save($group);
        }
    }

    function setAcos() {
        $aco = new Aco();
        $groups = array(
            0 => array('alias' => 'articles'),
        );

        foreach($groups as $group) {
            $aco->create();
            $aco->save($group);
        }
    }

    function setPermissions() {
//        $this->Acl->allow('admin','articles');
//        $this->Acl->deny('admin','articles','delete');
    }
}

?>